package com.agilegroup.crm.service;

import java.util.List;

import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import android.app.Application;

import com.octo.android.robospice.SpringAndroidSpiceService;
import com.octo.android.robospice.persistence.CacheManager;
import com.octo.android.robospice.persistence.exception.CacheCreationException;
import com.octo.android.robospice.persistence.memory.LruCacheStringObjectPersister;
import com.octo.android.robospice.persistence.springandroid.json.jackson.JacksonObjectPersisterFactory;

public class JsonSpiceService extends SpringAndroidSpiceService{

	@Override
	public CacheManager createCacheManager(Application application) {
		CacheManager cacheManager = new CacheManager();
		
		LruCacheStringObjectPersister memoryPersister = new LruCacheStringObjectPersister(1024 * 1024);
		cacheManager.addPersister(memoryPersister);
        
        
        JacksonObjectPersisterFactory jacksonObjectPersisterFactory;
		try {
			jacksonObjectPersisterFactory = new JacksonObjectPersisterFactory( application );
			cacheManager.addPersister( jacksonObjectPersisterFactory );
		} catch (CacheCreationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        return cacheManager;
	}
	
	@Override
	public RestTemplate createRestTemplate() {
		 RestTemplate restTemplate = new RestTemplate();
	        MappingJacksonHttpMessageConverter jsonConverter = new MappingJacksonHttpMessageConverter();
	        FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
	        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
	        final List< HttpMessageConverter< ? >> listHttpMessageConverters = restTemplate.getMessageConverters();

	        listHttpMessageConverters.add( jsonConverter );
	        listHttpMessageConverters.add( formHttpMessageConverter );
	        listHttpMessageConverters.add( stringHttpMessageConverter );
	        restTemplate.setMessageConverters( listHttpMessageConverters );
	        manageTimeOuts(restTemplate);
	        return restTemplate;
	}
	private void manageTimeOuts(RestTemplate restTemplate) {
	    // set timeout for requests
	    ClientHttpRequestFactory factory = restTemplate.getRequestFactory();
	    if (factory instanceof HttpComponentsClientHttpRequestFactory) {
	        HttpComponentsClientHttpRequestFactory advancedFactory = (HttpComponentsClientHttpRequestFactory) factory;
	        advancedFactory.setConnectTimeout(1000);
	        advancedFactory.setReadTimeout(1000);
	    } else if (factory instanceof SimpleClientHttpRequestFactory) {
	        SimpleClientHttpRequestFactory advancedFactory = (SimpleClientHttpRequestFactory) factory;
	        advancedFactory.setConnectTimeout(1000);
	        advancedFactory.setReadTimeout(1000);
	    }
	}
}
